//
//  WYIndustryController.m
//  Boss
//
//  Created by admin on 2018/7/5.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYIndustryController.h"
#import "Profession.h"
#import "WYProfession.h"
#import "WYSubProfession.h"
#import "WYProfessionSectionView.h"
@interface WYIndustryController ()<UITableViewDelegate,UITableViewDataSource,WYProfessionSectionViewDelegate>
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong)NSIndexPath *indexPath;
@end

@implementation WYIndustryController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
-(void)setupUI
{
    NSDictionary *dic = [Profession readLocalFileWithName:@"profession"];
    self.dataArray = [WYProfession getProfessionModelFronJson:dic];
    
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.title = @"所属行业";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:(UIBarButtonItemStyleDone) target:self action:@selector(doneAction:)];
    
    
}
-(void)doneAction:(UIBarButtonItem *)item
{
    if (!self.indexPath) {
        [self toast:@"请选择行业"]; return;
    }
    
    
    WYProfession *pro = self.dataArray[self.indexPath.section];
    WYSubProfession *sub =pro.subProfession[self.indexPath.row];
    WEAKSELF
    [WYHttpServer editMyFirmWithBrandName:nil WithFirmIntroduction:nil WithFirmType:sub.parentName WithFirmLogo:nil SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
             [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
        
        
        
    }];
    
    
    
    
    
    
//    [WYHttpServer editdutyWithProfession:sub.parentName SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
//        if (success) {
//              [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshMessageDate" object:nil];
//            [weakSelf.navigationController popViewControllerAnimated:YES];
//
//        }else{
//            [weakSelf toast:errorMessage];
//        }
//
//
//    }];
    
    
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    WYProfession *pro = self.dataArray[section];
    return pro.isOpen?pro.subProfession.count : 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    WYProfession *pro = self.dataArray[indexPath.section];
    WYSubProfession *sub =pro.subProfession[indexPath.row];
    cell.textLabel.text =[NSString stringWithFormat:@"   %@" ,sub.parentName];
    cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_location_select"]];
    if (indexPath==_indexPath) {
        cell.accessoryView.hidden = NO;
    }else{
        cell.accessoryView.hidden = YES;
    }
    cell.backgroundColor = [UIColor hexStringToColor:@"#fffaf2" alpha:1.0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYProfessionSectionView *headView = [WYProfessionSectionView headFootViewTableView:tableView section:section];
    headView.model = self.dataArray[section];
    headView.delegate = self;
    return headView;
}
-(void)reloadTableView:(NSInteger)section
{
//    WYProfession *pro = self.dataArray[section];
//    pro.isOpen = !pro.isOpen;
    
    [self.tableView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *celled = [tableView cellForRowAtIndexPath:_indexPath];
    celled.accessoryView.hidden=YES;
    _indexPath = indexPath;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView.hidden=NO;
    
    
}





-(UITableView *)tableView
{
    
    if (!_tableView) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    
    
    
    
    
    return _tableView;
}








@end
